package com.forezp.configclient.rabbitMq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @version V1.0  </br>
 * @ClassName Send </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 11/22/2018 5:44 PM</br>
 */
public class Send {
    //队列名称
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args){
        /**
         * 创建连接连接到MabbitMQ
         */
        ConnectionFactory connectionFactory = new com.rabbitmq.client.ConnectionFactory();
        //设置MabbitMQ所在主机ip或者主机名
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("root");
        connectionFactory.setVirtualHost("/");
        //创建一个连接
        try {
            Connection connection = connectionFactory.newConnection();
            //创建一个频道
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            String message = "hello wangt!";
            //往队列中发出一条消息
            channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
            //关闭频道和连接
            channel.close();
            connection.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
